#include "../Matrix.h"
#include "GeometricData.h"

#ifndef _Contour_h
#define _Contour_h

class DataPoint
{
 public:
  double x;
  double y;
  
  DataPoint* pPreviousDataPoint;
  DataPoint* pNextDataPoint;
  
  DataPoint();
  ~DataPoint();
};

class Contour
{
 private:
  bool InsertPoint( DataPoint* AfterMe, DataPoint* InsertMe );
  bool RemovePoint( DataPoint* RemoveMe );

  // These operations are a little "dangerous".
  // Only allow them to be called at prescribed times.
  
  bool SortPoints( void );
  bool SeparateContourPieces( void );
  bool CloseContour( void );
  bool UncloseContour( void );
  bool ReverseContour( void );
  bool IsPositivelyOriented( Matrix& Xin, Matrix& Yin, Matrix& Input );
  
  // These aren't terribly dangerous, but I'd feel more comfortable
  // only using them withing the "initialization" context.

  bool CompletelyOrganizeIntoClosedContours( void );
  bool PositivelyOrientContour( Matrix& Xin, Matrix& Yin, Matrix& Input );
  bool RemoveIsolatedContours( void );
  bool RemoveBumps( void );
  
 public: 
  DataPoint* pFirstDataPoint;
  double Value;
 
  Contour* pPreviousContour;
  Contour* pNextContour;

  Contour();
 ~Contour();
 
 // This function finds the contour, sorts it, separates it into separate contours, 
 // closes the curves, and orients them positively.
 
 bool InitializeFromMatrix( Matrix& Xin, Matrix& Yin, Matrix& Input , double value );

 // This function outputs only the current portion of the contour. 

 bool Output( const char* xfile, const char* yfile );
 
 // This function cleans up the contour by removing any additional linked 
 // pieces, as well as the data points. All that remains is the head data point
 
 bool CleanupContour( void );
 
 // This finds the closest point on the contour.
 
 DataPoint* FindClosestContourDataPoint( double x, double y ); 
 
 double GetClosestDistance( double x, double y );
 GeometricDataPacket GetGeometricData( double x, double y );
};

#endif
